<?php
namespace app\wechat\controller;
use app\api\service\Dealer;

class DCertification extends WCommon
{
    // 申请页面
    public function index()
    {
        $member_id = session('member_id');

        $dealer = db('dealer')->alias('de')
            ->field('de.*, p.region_name r_province, c.region_name r_city, e.region_name r_area, dap.region_name dap_province, dac.region_name dac_city, dae.region_name dae_area, da.province da_province, da.city da_city, da.area da_area')
            ->join('mob_region p', 'p.region_id = de.province', 'LEFT')
            ->join('mob_region c', 'c.region_id = de.city', 'LEFT')
            ->join('mob_region e', 'e.region_id = de.area', 'LEFT')
            ->join('mob_duty_area da', 'da.member_id = de.member_id AND da.member_type = 2', 'LEFT')
            ->join('mob_region dap', 'dap.region_id = da.province', 'LEFT')
            ->join('mob_region dac', 'dac.region_id = da.city', 'LEFT')
            ->join('mob_region dae', 'dae.region_id = da.area', 'LEFT')
            ->where('de.member_id', $member_id)->find();
        //dump($dealer);
        if (isset($dealer['audit_status']) && $dealer['audit_status'] == 4) {
            $this->redirect(url('d_member/index'));
        }

        //pj170329add多图弄成数组
        if(!is_weixin()){
            $dealer["environment_img"] = array();
            if($dealer["environment_img1"]){$dealer["environment_img"][] = $dealer["environment_img1"];}
            if($dealer["environment_img2"]){$dealer["environment_img"][] = $dealer["environment_img2"];}
            if($dealer["environment_img3"]){$dealer["environment_img"][] = $dealer["environment_img3"];}
            if($dealer["environment_img4"]){$dealer["environment_img"][] = $dealer["environment_img4"];}
            if($dealer["environment_img5"]){$dealer["environment_img"][] = $dealer["environment_img5"];}
            //unset($dealer["environment_img1"],$dealer["environment_img2"],$dealer["environment_img3"],$dealer["environment_img4"],$dealer["environment_img5"]);
        }


        $this->assign('dealer', $dealer);
        return view();
    }

    // 咨询页面
    public function consult()
    {
        return view();
    }

    // 提交申请
    public function certification() {
        if (request()->isAjax()) {
            if (!input('?param.duty_area') || !input('param.duty_area')) {
                return json(['code' => 400111, 'msg' => '请选择意向区域']);
            }
            $duty_area = explode(',', input('param.duty_area'));

            if (!input('?param.level') || !input('param.level')) {
                return json(['code' => 400112, 'msg' => '请选择意向等级']);
            }

            // 获取申请等级
            switch (input('param.level', '')) {
                case '省':
                    $level = 1;
                    $duty_area[1] = $duty_area[2] = 0;
                    break;
                case '市';
                    $level = 2;
                    $duty_area[2] = 0;
                    break;
                case '区/县';
                    $level = 3;
                    break;
                default:
                    $level = 0;
                    break;
            }

            $param = [
                'id' => input('param.id', 0),
                'member_id' => session('member_id'),
                'dealer_name' => input('param.dealer_name'),
                'proposer' => input('param.proposer'),
                'card_no' => input('param.card_no'),
                'id_card_in_hand' => input('param.id_card_in_hand'),
                'id_card_front' => input('param.id_card_front'),
                'id_card_verso' => input('param.id_card_verso'),
                'level' => $level,

                'duty_province' => $duty_area[0],
                'duty_city' => $duty_area[1],
                'duty_area' => $duty_area[2],

                'province' => input('param.province'),
                'city' => input('param.city'),
                'area' => input('param.area'),
                'street' => input('param.street'),
                
                'address' => input('param.address'),

                'business_license_name' => input('param.business_license_name'),
                'business_license_num' => input('param.business_license_num'),
                'business_license' => input('param.business_license'),

                'lat' => input('param.lat'),
                'lon' => input('param.lon'),
                
                'phone' => input('param.phone'),
                'logo' => input('param.logo'),
                'cover' => input('param.cover'),
                
                'environment_img1' => input('param.environment_img1', ''),
                'environment_img2' => input('param.environment_img2', ''),
                'environment_img3' => input('param.environment_img3', ''),
                'environment_img4' => input('param.environment_img4', ''),
                'environment_img5' => input('param.environment_img5', ''),

                'collecting_note' => input('param.collecting_note'),
            ];

            $data = Dealer::dealer_create($param);
            return json($data);
        } else {
            return view('public/error');
        }
    }

    // 申请状态查询
    public function audit_status()
    {
        $status = input('param.status', 0);
        $msg = input('param.msg', '');
        $this->assign('status', $status);
        $this->assign('msg', $msg);
        return view();
    }
}
